<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="submitForm('ruleForm')">保存</el-button>
            </div>

            <div class="table-form-box">
                <h3 class="h3">往来单位</h3>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                    <table border="0" width="100%" cellpadding="0" cellspacing="1">
                        <colgroup>
                            <col width="15%"/>
                            <col width="35%"/>
                            <col width="15%"/>
                            <col width="35%"/>
                        </colgroup>
                        <tbody>
                            <tr class="tr-title">
                                <td colspan="8">基本信息</td>
                            </tr>
                            <tr>
                                <td align="right">类别编号:</td>
                                <td>
                                     <el-form-item prop="linkName">
                                        <el-input
                                            v-model="ruleForm.linkName"
                                            size="small"
                                            disabled
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right"><span class="color-ff2200">*</span>类别名称</td>
                                <td> 
                                    <el-form-item prop="name">
                                        <el-input
                                            v-model="ruleForm.name"
                                            size="small"
                                     
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                
                            </tr>
                            <tr>
                                <td align="right">
                                   上级分类:
                                </td>
                                <td>
                                    <el-form-item prop="parentName">
                                        <div @click="showunitSearchDialog('上级分类')">
                                            <el-input
                                                placeholder="请输入"
                                                v-model="ruleForm.parentName"
                                                size="small"
                                                readonly
                                            >
                                                <i slot="suffix" class="el-icon-search"></i>
                                            </el-input>
                                        </div>
                                    </el-form-item>
                                </td>
                                <td align="right"></td>
                                <td></td>
                            </tr>
                            
                            <tr>
                                <td align="right">备注:</td>
                                <td colspan="8">
                                    <el-form-item prop="remark">
                                        <el-input v-model="ruleForm.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </el-form>
            </div>

            <!-- 上传附件 -->
            <!-- <upload-files :files="fileList" @fun="dealFiles" v-if="flag"></upload-files> -->
        </div>
        <!-- 用户树搜索 -->
        <transit-unit-search
            :isShow="showUnitSearch"
            :dialogTitle="unitSearchDialogTit"
            @edit="unitSearchBack"
            @close="closeDialog"
        ></transit-unit-search>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
import bus from '../../../components/common/bus'
 var checkPhone = (rule, value, callback) => {
      if (value !== '' && !(/^1[345789]\d{9}$/.test(value))) {
        callback(new Error('请输入正确的手机号'));
      } else{
        callback();
      }
    };


export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            unitSearchDialogTit: '',
            showUnitSearch: false,
            ruleForm: {
                name: '', // 往来单位类别名称	
                parentId: 0, // 父类别id	
                typeNo: '', // 往来单位编码
                parentName: '' 
            },
            rules: {
                name:[{ required: true, message: '请输入类别名称', trigger: 'blur' }],
            },
            fileList: [],
            isUpdate: false, //是否是更新
            flag: false,
        };
    },
    methods: {
        // 选择姓名
        showunitSearchDialog(tit) {
            this.unitSearchDialogTit = tit;
            this.showUnitSearch = true;
        },
        // 责任人搜索结果返回
        unitSearchBack(data) {
            console.log(data.backData, 'unitSearchBack');
            this.ruleForm.parentName = data.backData.name;
            this.ruleForm.parentId = data.backData.id;
            this.showUnitSearch = false;
        },
        closeDialog (data) {
            this.showUnitSearch = false
        },
        //新增
        clientTypeInsert() {
            appMain
                .clientTypeInsert(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        bus.$emit('close_current_tags')
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 更新
        clientTypeUpdateById() {
            appMain
                .clientTypeUpdateById(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        bus.$emit('close_current_tags')
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        clientTypeFindById(id) {
            appMain
                .clientTypeFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.ruleForm = res.data.data;
                        // 再查一下上级分类的信息，显示上级分类的名字
                        if (this.ruleForm.parentId) {
                            this.findParentType(res.data.data.parentId);
                        }
                    } else {
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 更新时查找上级分类
        findParentType (id) {
            appMain
                .clientTypeFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$set(this.ruleForm, 'parentName',  res.data.data.name )
                        
                    } else {
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //根据id查询附件
        uploadFileInfoFindById(id) {
            appMain
                .uploadFileInfoFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.fileList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                    this.flag = true;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.ruleForm.uploadReturns = this.fileList;
                    //alert('submit!');
                    if (!this.isUpdate) {
                        //添加文件列表
                        //新增
                        this.clientTypeInsert();
                    } else {
                        //添加文件列表
                        //更新
                        this.clientTypeUpdateById();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 上传文件返回
        dealFiles(data) {
            this.fileList = data.backData;
            this.ruleForm.uploadReturns = this.fileList;
        }
    },

    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    created() {
        //如果有id过来就是修改的
        if (this.$route.query.id) {
            let id = this.$route.query.id;
            //是更新操作
            this.isUpdate = true;
            //通过id查询对应离职信息
            this.clientTypeFindById(id);
            //通过id查询附件信息
            this.uploadFileInfoFindById(id);
        } else {
            //新增操作
            this.isUpdate = false;
            this.flag = true;
        }
    }
};
</script>

<style  scoped>
.container {
    padding-bottom: 50px;
}
.handle-box {
    padding: 5px 50px;
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.table-form-box table {
    background-color: #666;
}
.table-form-box .h3 {
    font-size: 22px;
    text-align: center;
    font-weight: normal;
    padding: 20px 0;
}
.table-form-box table .tr-title td {
    font-size: 14px;
    line-height: 40px;
}
.table-form-box table td,
.table-form-box table th {
    background-color: #fff;
    padding: 4px;
    min-height: 40px;
    font-size: 12px;
}
.color-ff2200 {
    color: #ff2200;
}
</style>